<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20"> <svg-icon icon-class="gongsi" /> <span>江苏传智播客教育科技股份有限公司</span> </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <!-- 一级部门的pid 就是'' -->
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体 -->
        <el-tree :data="list" :default-expand-all="true" :props="defaultProps">
          <template slot-scope="{ data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 添加和修改的弹窗 -->
    <el-dialog
      v-if="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="isEdit? '编辑部门' : '添加部门'"
      :visible.sync="showDialog"
      @close="$refs.refDeptDialog.resetForm()"
    >
      <DeptDialog
        ref="refDeptDialog"
        :origin-list="originList"
        :pid="curId"
        :is-edit="isEdit"
        @close="showDialog=false"
        @success="hSuccess"
      />
    </el-dialog>
  </div>
</template>

<script>
import { arrayToTree } from '@/utils'
import { delDepartment, getDepartments } from '@/api/department'
import DeptDialog from './deptDialog.vue'
export default {
  components: {
    DeptDialog
  },
  data() {
    return {
      isEdit: false, // 是否处于编辑状态
      // 当前选中部门的id
      curId: '',
      // 控制对话框显示和隐藏
      showDialog: false,
      // 保存数据
      list: [],
      // 设置具体的替换类名
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 存储需要做 校验的数据
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 收到子组件中的success事件
    hSuccess() {
      // 关闭弹层，更新数据
      this.showDialog = false
      // 更新数据
      this.loadDepartments()
    },
    // 用户点击了 删除
    hDel(id) {
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 调接口删除数据
        await delDepartment(id)
        // 删除成功提示
        this.$message.success('删除成功')
        // 更新数据
        await this.loadDepartments()
      }).catch(() => {
      })
    },
    // 用户点击了 编辑
    hEdit(id) {
      // 更新id
      this.curId = id
      // 设置状态为编辑
      this.isEdit = true
      // 打开弹窗
      this.showDialog = true
    },
    // 用户点击了 添加
    hAdd(id) {
      // 显示弹窗
      this.showDialog = true
      // 拿到当前id 传给子组件
      // console.log(data.id)
      this.curId = id
      this.isEdit = false
    },
    // 请求部门列表
    async loadDepartments() {
      const { data: res } = await getDepartments()
      // console.log(res)
      // 把数组第一元素删除，剩余存到list
      res.depts.shift()
      // 拿到做表单校验 需要的数据
      this.originList = res.depts.map(item => {
        return {
          id: item.id,
          pid: item.pid,
          name: item.name,
          code: item.code
        }
      })
      // 数组转树
      this.list = arrayToTree(res.depts)
    }
  }
}
</script>
